package com.tarena.lbs.user.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.user.dao.mapper.UserGroupMapper;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.pojo.param.UserGroupParam;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserGroupRepositoryImpl implements UserGroupRepository {
    private static final Logger log = LoggerFactory.getLogger(UserGroupRepositoryImpl.class);
    @Autowired
    private UserGroupMapper userGroupMapper;

    @Override
    public PageInfo<UserGroupPO> pageList(UserGroupQuery query, Integer businessId) {
        QueryWrapper<UserGroupPO> queryWrapper = new QueryWrapper<>();
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        queryWrapper.eq("business_id", businessId);
        if (query.getStoreName() != null) {
            queryWrapper.like("group_name", query.getStoreName());
        }
        List<UserGroupPO> pos = userGroupMapper.selectList(queryWrapper);
        log.debug("数据访问层,分页查询用户列表,pos={}", pos);
        return new PageInfo<>(pos);
    }

    @Override
    public void insert(UserGroupPO userGroupPO) {
        userGroupMapper.insert(userGroupPO);
    }

    @Override
    public List<UserGroupPO> selectByBusinessIdAndTagIds(Integer businessId, List<Integer> tagIds) {
        QueryWrapper<UserGroupPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", businessId);
        queryWrapper.in("tag_id", tagIds);
        List<UserGroupPO> pos = userGroupMapper.selectList(queryWrapper);
        return pos;
    }

    @Override
    public UserGroupPO getById(Integer id) {
        return userGroupMapper.selectById(id);
    }

    @Override
    public void updateById(UserGroupParam param) {
        UserGroupPO userGroupPO = new UserGroupPO();
        BeanUtils.copyProperties(param, userGroupPO);
        userGroupMapper.updateById(userGroupPO);
    }
}
